package com.juzipi.demo.controller;


import com.juzipi.demo.pojo.Users;
import com.juzipi.demo.service.AdminService;
import com.juzipi.demo.tools.AdminLoginParam;
import com.juzipi.demo.tools.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
@Api(tags = "LoginController")
public class LoginController {

    @Autowired
    private AdminService adminService;


    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(AdminLoginParam adminLoginParam, HttpServletRequest request){

        return adminService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword(),request);

    }


    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/admin/info")
    public Users getUsersInfo(Principal principal){
        if (principal == null){
            return null;
        }
        String username = principal.getName();
        Users users = adminService.getAdminByUserName(username);
        users.setPassword(null);
        return users;

    }



    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout(){

        return RespBean.success("退出登录");

    }



}
